import uniq from "lodash/uniq";
import { LogoutIcon } from "tdesign-icons-vue-next";
import { shallowRef } from "vue";
import {
  createRouter,
  createWebHistory,
  RouteRecordRaw,
  useRoute,
} from "vue-router";

import Layout from "@/layouts/index.vue";
const env = import.meta.env.MODE || "development";

// 导入homepage相关固定路由
const homepageModules = import.meta.glob("./modules/**/homepage.ts", {
  eager: true,
});

// 导入modules非homepage相关固定路由
// const fixedModules = import.meta.glob('./modules/**/!(homepage).ts', { eager: true });
const fixedModules = import.meta.glob("./modules/**/!(homepage).ts", {
  eager: false,
});

// 其他固定路由
const defaultRouterList = [
  {
    path: "/",
    redirect: "/dashboard/base",
    component: Layout,
    children: [
      {
        path: "login",
        name: "login",
        component: () => import("@/pages/login/index.vue"),
        meta: { title: "登录" },
      },
      {
        path: "introduce",
        name: "introduce",
        component: () => import("@/pages/introduce/introduce.vue"),
        meta: { title: "赛事介绍" },
      },
      {
        path: "aboutUs",
        name: "aboutUs",
        component: () => import("@/pages/aboutUs/index.vue"),
        meta: { title: "关于我们" },
      },
      {
        path: "news",
        name: "news",
        component: () => import("@/pages/news/index.vue"),
        meta: { title: "新闻公告" },
      },
      {
        path: "newsIndex",
        name: "newsIndex",
        component: () => import("@/pages/news/index.vue"),
        meta: { title: "新闻公告" },
      },
      {
        path: "newsDetail",
        name: "newsDetail",
        component: () => import("@/pages/news/detail.vue"),
        meta: { title: "新闻详情" },
      },
      {
        path: "signUp",
        name: "signUp",
        component: () => import("@/pages/signUp/signUp.vue"),
        meta: { title: "比赛报名" },
      },
      {
        path: "signUpSubmit",
        name: "signUpSubmit",
        component: () => import("@/pages/signUp/components/signUpSubmit.vue"),
        meta: { title: "参赛报名表" },
      },
      // 证书验证
      {
        path: "certificateQuery",
        name: "certificateQuery",
        component: () =>
          import("@/pages/certificateQuery/certificateQuery.vue"),
        meta: { title: "证书验证" },
      },
      {
        path: "contactUs",
        name: "contactUs",
        component: () => import("@/pages/contactUs/contactUs.vue"),
        meta: { title: "联系我们" },
      },
      {
        path: "user",
        name: "user",
        component: () => import("@/pages/userInfo/index.vue"),
        meta: { title: "个人中心" },
      },
      {
        path: "userMobile",
        name: "userMobile",
        component: () => import("@/pages/userInfo/mobile.vue"),
        meta: { title: "个人中心" },
      },
    ],
  },
];
// 存放固定路由
export const homepageRouterList: Array<RouteRecordRaw> =
  mapModuleRouterList(homepageModules);
export const fixedRouterList: Array<RouteRecordRaw> =
  mapModuleRouterList(fixedModules);

export const allRoutes = [
  ...homepageRouterList,
  ...fixedRouterList,
  ...defaultRouterList,
];

// 固定路由模块转换为路由
export function mapModuleRouterList(
  modules: Record<string, unknown>
): Array<RouteRecordRaw> {
  const routerList: Array<RouteRecordRaw> = [];
  Object.keys(modules).forEach((key) => {
    // @ts-ignore
    const mod = modules[key].default || {};
    const modList = Array.isArray(mod) ? [...mod] : [mod];
    routerList.push(...modList);
  });
  return routerList;
}

export const getRoutesExpanded = () => {
  const expandedRoutes = [];

  fixedRouterList.forEach((item) => {
    if (item.meta && item.meta.expanded) {
      expandedRoutes.push(item.path);
    }
    if (item.children && item.children.length > 0) {
      item.children
        .filter((child) => child.meta && child.meta.expanded)
        .forEach((child: RouteRecordRaw) => {
          expandedRoutes.push(item.path);
          expandedRoutes.push(`${item.path}/${child.path}`);
        });
    }
  });
  return uniq(expandedRoutes);
};

export const getActive = (maxLevel = 3): string => {
  const route = useRoute();
  if (!route) {
    return "";
  }
  if (!route.path) {
    return "";
  }
  return route.path
    .split("/")
    .filter((_item: string, index: number) => index <= maxLevel && index > 0)
    .map((item: string) => `/${item}`)
    .join("");
};

const router = createRouter({
  history: createWebHistory(
    env === "site" ? "/starter/vue-next/" : import.meta.env.VITE_BASE_URL
  ),
  routes: allRoutes,
  scrollBehavior() {
    return {
      el: "#app",
      top: 0,
      behavior: "smooth",
    };
  },
});

export default router;
